$:.unshift File.join(File.dirname(__FILE__),'..','lib')
require 'test/unit'
require 'model'

class TcAccount < Test::Unit::TestCase
  
  def test_current_balance
    asset = Account.new("Test", :debit, nil)
    assert_equal(0.0, asset.current_balance)
    asset.journal(:debit, 123)
    assert_equal(123, asset.current_balance)
    asset.journal(:credit, 99)
    assert_equal(24, asset.current_balance)
    
    liability = Account.new("Test", :credit, nil)
    assert_equal(0, liability.current_balance)
    liability.journal(:credit, 567)
    assert_equal(567, liability.current_balance)
    liability.journal(:debit, 12)
    assert_equal(555, liability.current_balance)
  end

  def test_journaling_to_parent_account
    parent = Account.new("Parent", :debit, nil)
    child_one = Account.new("Child One", :debit, parent)
    child_two = Account.new("Child Two", :debit, parent)

    assert_equal(0, child_one.current_balance)
    assert_equal(0, child_two.current_balance)
    assert_equal(0, parent.current_balance)

    child_one.journal(:debit, 123)
    child_two.journal(:debit, 456)
    assert_equal(123, child_one.current_balance)
    assert_equal(456, child_two.current_balance)
    assert_equal(579, parent.current_balance)

    child_one.journal(:credit, 99)
    child_two.journal(:credit, 99)
    assert_equal(24, child_one.current_balance)
    assert_equal(357, child_two.current_balance)
    assert_equal(381, parent.current_balance)
  end
end
